﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Components;
using Components.Enum;
using Model;
using ServiceHelper.Util;

namespace ServiceHelper.Doc
{
    public class SendManager : DocumentManager
    {
        public override Sto8a00 CreateDefaultModel()
        {
            var model = new Sto8a00();
            model.Doctype = DocumentType.SendDoc;
            return model;
        }

        public override string Save()
        {
            if (0 == Doc.Rows.Count)
                return "本次发货数量为0，无需保存";

            return base.Save();
        }

        public Sto8a01 ParseRow(DataRow row)
        {
            var model = new Sto8a01();

            model.Merchid = Validation.ToString(row["pdlistid"]);
            model.Dtlrefno1 = Validation.ToString(row["listid"]);
            model.Spno = Validation.ToString(row["spno"]);
            model.Startbuyqty = Validation.ToDouble(row["bordqty"]);
            model.Dtlqty = Validation.ToDouble(row["dtlqty"]); //本次发货量
            model.Actqty2 = Validation.ToDouble(row["actqty2"]); //发货赠品数
            model.Dtlprice = Validation.ToDouble(row["ordprice"]); //发货价格
            model.Cess = Validation.ToDouble(row["cess"]);
            model.Dtlremark = Validation.ToString(row["dtlremark1"]);
            model.Actamt3 = Validation.ToDouble(row["dtltotalamt"]); //含税总额

            model.Dtlstockqty = model.Dtlqty + model.Actqty2; //用于记账的库存

            if (Validation.ToFloat(model.Dtlstockqty) == 0 && Validation.ToFloat(model.Dtlqty) == 0) //没有输入数量的行跳过
            {
                return null;
            }

            Doc.Rows.Add(model);

            return model;
        }

        /// <summary>
        /// 将概要信息转换成Sto8a00
        /// </summary>
        /// <param name="tb"></param>
        public Sto8a00 ParseDoc(Hashtable tb)
        {
            var sto8A00 = this.CreateDefaultModel();
            sto8A00.Warehouseid = Validation.ToString(tb["storageid"]);

            if (string.IsNullOrEmpty(sto8A00.Warehouseid))
                throw new Exception("未设置发货仓库，不能做发货操作");

            sto8A00.Docno = Validation.ToString(tb["sendocno"]);

            if (!string.IsNullOrEmpty(sto8A00.Docno))
            {
                var qb = QB.Entity.AddParam("docno", sto8A00.Docno)
                                  .AddParam("doctype", DocumentType.SendDoc)
                                  .AddParam("%usercode", string.Format(" like '{0}.%' ", ServerContext.ComCode));

                //验证发货单重复性
                if (CommonManager.Entity.HasData("Sto8a00", qb))
                {
                    var hsWhere = QB.Entity.AddParam("muser", ServerContext.ComCode + ".");

                    var lastno = CommonManager.Entity.ExecDbFunScalar("getlastsendno", hsWhere);

                    var o = new { status = false, msg = string.Format("发货单号{0}重复，请重新输入", sto8A00.Docno), last = lastno };
                    throw new Exception(Json.ToString(o));
                }
            }

            sto8A00.Refid = Validation.ToString(tb["listid"]);
            sto8A00.Refdocno = Validation.ToString(tb["docno"]);
            sto8A00.Docstatus = DocumentStatus.Normal;

            return sto8A00;
        }

        protected override void AfterSubmit()
        {
            var exec = Exec.Create();

            //更新商品表最新发货时间, 累加在途量
            foreach (var row in Doc.Rows)
            {
                exec.AddSql(Pot3a00Manager.Entity.UpdateWayQty(row.Merchid, row.Dtlstockqty));

                if (string.IsNullOrEmpty(row.Dtlrefno1))
                    continue;

                exec.AddSql(Ord3a01Manager.Entity.SendRow(row.Dtlrefno1, row.Dtlqty, row.Actqty2, row.Actamt3));
            }

            //更新发货时间和累计发货额和数量
            exec.AddSql(Ord3a00Manager.Entity.UpdateSendAmt(Doc.Refid));

            exec.Do();
        }
    }
}
